package com.atguigu.lease.web.admin.controller;

import com.atguigu.lease.common.result.Result;
import com.atguigu.lease.model.entity.ApartmentInfo;
import com.atguigu.lease.model.enums.ReleaseStatus;
import com.atguigu.lease.web.admin.service.ApartmentService;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentSubmitVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: 李壮
 * @CreateTime: 2025-05-07
 * @Version: 1.0
 */
@Tag(name = "公寓信息管理")
@RestController
@RequestMapping("/admin/apartment")
public class ApartmentController {

    @Autowired
    ApartmentService apartmentService;

    @Operation(summary = "根据id修改公寓发布状态")
    @PostMapping("/updateReleaseStatusById")
    public Result updateReleaseStatusById(@RequestParam Long id, ReleaseStatus releaseStatus){

        LambdaUpdateWrapper<ApartmentInfo> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(ApartmentInfo::getId,id);
        lambdaUpdateWrapper.set(ApartmentInfo::getIsRelease,releaseStatus);
        apartmentService.update(lambdaUpdateWrapper);
        return Result.ok();
    }

    @Operation(summary = "保存或更新公寓信息")
    @PostMapping("/saveOrUpdate")
    public Result saveOrUpdate(@RequestBody ApartmentSubmitVo apartmentSubmitVo){

        apartmentService.saveOrUpdateApartment(apartmentSubmitVo);
        return Result.ok();
    }

    @Operation(summary = "根据条件分页查询公寓列表")
    @GetMapping("/pageItem")
    public Result pageItem(@RequestParam Long current, @RequestParam Long size, ApartmentQueryVo apartmentQueryVo){

        IPage<ApartmentItemVo> page = new Page<>(current,size);
        IPage<ApartmentItemVo> list = apartmentService.pageApartmentItemByQuery(page,apartmentQueryVo);
        return Result.ok(list);
    }

    @Operation(summary = "根据ID获取公寓详细信息")
    @GetMapping("/根据ID获取公寓详细信息")
    public Result getDetailById(@RequestParam Long id){

        ApartmentDetailVo apartmentDetailVo = apartmentService.getApartmentDetailByid(id);
        return Result.ok(apartmentDetailVo);
    }

    @Operation(summary = "根据ID删除公寓信息")
    @DeleteMapping("/removeById")
    public Result removeById(@RequestParam Long id){
        apartmentService.removeAparymentById(id);
        return Result.ok();
    }

    @Operation(summary = "根据区县ID查询公寓信息列表")
    @GetMapping("/listInfoByDistrictId")
    public Result listInfoByDistrictId(@RequestParam Long id){

        List<ApartmentInfo> list = apartmentService.list(new LambdaQueryWrapper<ApartmentInfo>()
                .eq(ApartmentInfo::getDistrictId, id));

        return Result.ok(list);
    }
}
